﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebRSSBuilder.Web;
using WebRSSBuilder.Helpers;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel; 

namespace WebRSSBuilder.Models
{
    public class FileViewModel : INotifyPropertyChanged
    {
        private DomainServiceDB Service;
        private File selectedItem { get; set; }
        public File SelectedItem
        {
            get
            {
                return this.selectedItem; 
            }
            set
            {
                this.selectedItem = value;
                NotifyPropertyChanged("SelectedItem"); 
            }
        }
        public ObservableCollection<File> FilesSource { get; set; }
        public ObservableCollection<aspnet_Roles> RolesSource { get; set; }
        public DelegateCommand LoadCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public FileViewModel()
        {
            Service  = new DomainServiceDB();
            LoadCommand = new DelegateCommand(Load, (obj) => { return true; }); 
            SaveCommand = new DelegateCommand(Save, (obj) => { return true; });
            LoadCommand.Execute(null); 
        }
        private void Load(object Parameter)
        {
            Service.Load(Service.GetFilesQuery(), LoadOperation=> {LoadFilesCompleted();}, null);
            Service.Load(Service.GetAspnet_RolesQuery(), LoadOperation => { LoadRolesCompleted(); }, null);
        }

        private void LoadRolesCompleted()
        {
            RolesSource = new ObservableCollection<aspnet_Roles>(Service.aspnet_Roles);
            NotifyPropertyChanged("RolesSource");
        }

        private void LoadFilesCompleted()
        {
            FilesSource = new ObservableCollection<File>(Service.Files);
            FilesSource.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(FilesSource_CollectionChanged);
            if (FilesSource.Count > 0)
                SelectedItem = FilesSource[0]; 
            NotifyPropertyChanged("FilesSource");
        }

        void FilesSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    Service.Files.Add((File)e.NewItems[0]);
                break; 
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                Service.Files.Remove((File)e.OldItems[0]);
                break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                Service.Files.Remove((File)e.OldItems[0]);
                Service.Files.Add((File)e.NewItems[0]);
                break; 


                
            }
        }
        private void Save(object Parameter)
        {
           // File f= new File() ; 
           // f.FileName = "test"; 
           // f.FileURL="test url";
           //// f.RoleID = new Guid("e74c8191-7696-4d42-b517-dd8f11bf44d6");
           // Service.Files.Add(f);  
            Service.SubmitChanges(); 
            
        }
        #region PropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string PropertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

            }
        }
        #endregion
    }
}
